package com.lvtongka.app.base

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.lvtongka.app.dialog.LoadingDialog
import com.lvtongka.app.http.common.WebConstants
import com.lvtongka.app.ui.webview.WebViewActivity

abstract class BaseVBActivity<T : ViewBinding> : AppCompatActivity() {
    private val minCLickDelayTime: Long = 500 // 时间阈值，单位毫秒
    private var lastClickTime: Long = 0
    lateinit var binding: T
    private var loadingDialog: LoadingDialog? = null
    // 控制状态栏颜色
    protected var isBlack: Boolean = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = initBinding()
        setContentView(binding.root)
        setStatusBarFullTransparent(this)
        loadingDialog = LoadingDialog(this)
    }

    protected abstract fun initEvent()

    protected abstract fun initData()

    protected abstract fun initBinding(): T

    open fun setStatusBarFullTransparent(activity: Activity) {
        if (Build.VERSION.SDK_INT >= 21) { //21表示5.0
            val window = activity.window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            if (isBlack) {
                window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
            } else {
                window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            }
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
        } else if (Build.VERSION.SDK_INT >= 19) { //19表示4.4
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

    @SuppressLint("ShowToast")
    open fun showToast(msg: String?) {
        runOnUiThread {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        }
    }

    open fun showLoading(msg: String= "加载中...") {
        loadingDialog?.let {
            it.setMessage(msg)
            it.setCancelable(false)
            it.setCanceledOnTouchOutside(false)
            it.show()
        }
    }

    open fun hideLoading() {
        loadingDialog?.let {
            if (it.isShowing) {
                it.dismiss()
            }
        }
    }

    // 快速点击
    protected fun isFastClick(): Boolean {
        var flag = false
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime >= minCLickDelayTime) {
            flag = true
        }
        lastClickTime = currentTime
        return !flag
    }

    protected fun jumpWeb(url: String, isWhite: Boolean = false) {
        val intent = Intent(this, WebViewActivity::class.java)
        intent.putExtra(WebViewActivity.URL_KEY, url)
        intent.putExtra(WebViewActivity.IS_WITHE, isWhite)
        startActivity(intent)
    }
}